<template>
  <div style="margin:10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="90px">
      <el-form-item label="支付订单ID" prop="payId">
        <el-input
          style="width: 150px"
          v-model="queryParams.payId"
          placeholder="请输入支付订单ID"
          clearable/>
      </el-form-item>
      <el-form-item label="退款单号" prop="orderNo">
        <el-input style="width: 140px"
                  v-model="queryParams.orderNo"
                  placeholder="请输入退款单号"
                  clearable/>
      </el-form-item>
      <el-form-item label="退款状态" prop="status">
        <el-select style="width: 145px" v-model="queryParams.type"
                   placeholder="请选择支付类型" clearable>
          <el-option
            label="未退款"
            value="created">
          </el-option>
          <el-option
            label="已退款"
            value="refunded">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台单号" prop="platformOrderNo">
        <el-input style="width: 170px"
                  v-model="queryParams.platformOrderNo"
                  placeholder="请输入平台退款单号"
                  clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="refundList">
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="支付订单ID" align="center" prop="payId"/>
      <el-table-column label="退款单号" align="center" prop="orderNo"/>
      <el-table-column label="退款金额" align="center" prop="amount"/>
      <el-table-column label="退款状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 'created'" effect="dark" size="mini" type="danger">未退款</el-tag>
          <el-tag v-if="scope.row.status == 'refunded'" effect="dark" size="mini">已退款</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="平台退款单号" align="center" prop="platformOrderNo"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:refund:edit']">刷新状态
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:refund:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  </div>
</template>

<script>
  import {listRefund, getRefund, delRefund, addRefund, updateRefund} from "@/api/system/refund";

  export default {
    name: "Refund",
    data() {
      return {
        // 遮罩层
        loading: true,

        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 退款表格数据
        refundList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,

          payId: null,
          orderNo: null,
          amount: null,
          status: null,
          platformOrderNo: null,
          createTime: null,
          updateTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          payId: [
            {required: true, message: "支付订单不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询退款列表 */
      getList() {
        this.loading = true;
        listRefund(this.queryParams).then(response => {
          this.refundList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          payId: null,
          orderNo: null,
          amount: null,
          status: null,
          platformOrderNo: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        console.log('----刷新----')
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateRefund(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除退款编号为"' + row.id + '"的数据项？').then(function () {
          return delRefund(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
